﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_SEC_OBJECT_ROLE
// * CreateDate    : 2011/7/15 14:33:58
// * Creater       : CodeSmith
// * LastChangeDate: 2011/7/15 14:33:58
// * LastChanger   : 2011/7/15 14:33:58
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Sec
{
    /// <summary>
    /// SecObjectRole
    /// </summary>
    public partial class SecObjectRoleBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objSecObjectRoleEntity">Entity</param>
        /// <returns>Entity</returns>
        public static SecObjectRoleEntity GetSecObjectRole(SecObjectRoleEntity objSecObjectRoleEntity)
        {
            return DataAccess.SelectSingle<SecObjectRoleEntity>(objSecObjectRoleEntity);
        }

        public static List<SecObjectRoleEntity> GetObjectRoleByObjectID(string objectID)
        {
            if (string.IsNullOrEmpty(objectID))
            {
                return null;
            }
            SecObjectRoleEntity entity = new SecObjectRoleEntity();
            entity.ObjectId = objectID;
            return DataAccess.Select<SecObjectRoleEntity>(entity);
        }

        public static List<SecObjectRoleEntity> GetObjectRoleByRoleID(string roleID)
        {
            if (string.IsNullOrEmpty(roleID))
            {
                return null;
            }
            SecObjectRoleEntity entity = new SecObjectRoleEntity();
            entity.RoleId = roleID;
            return DataAccess.Select<SecObjectRoleEntity>(entity);
        }

        public static bool CheckUserObjectRight(string personId, string menuID, string objectCode)
        {
            string sql = @"SELECT SEC_OBJECT_ROLE.*
                            FROM SEC_OBJECT_ROLE 
                            INNER JOIN SEC_ROLE ON SEC_ROLE.ROLE_ID = SEC_OBJECT_ROLE.ROLE_ID
                            INNER JOIN SEC_USER_ROLE ON SEC_USER_ROLE.ROLE_ID = SEC_ROLE.ROLE_ID
							INNER JOIN SEC_USER ON SEC_USER.USER_ID = SEC_USER_ROLE.USER_ID
                            INNER JOIN SEC_OBJECT ON SEC_OBJECT.OBJECT_ID = SEC_OBJECT_ROLE.OBJECT_ID
                            WHERE SEC_USER.PERSON_ID = '" + personId + @"' 
								AND SEC_OBJECT.OBJECT_CODE = '" + objectCode + @"' 
								AND SEC_OBJECT.MENU_ID = '" + menuID + @"'
                            UNION ALL
                            SELECT SEC_OBJECT_ROLE.*
                            FROM SEC_OBJECT_ROLE 
                            INNER JOIN SEC_ROLE ON SEC_ROLE.ROLE_ID = SEC_OBJECT_ROLE.ROLE_ID
								AND SEC_ROLE.POSITION_ID IN (
								SELECT P.POSITION_ID FROM MST_POSITION P 
								INNER JOIN HR_PERSON_POSITION PP ON PP.POSITION_ID = P.POSITION_ID 
								WHERE PP.PERSON_ID = '" + personId + @"' )
                            INNER JOIN SEC_OBJECT ON SEC_OBJECT.OBJECT_ID = SEC_OBJECT_ROLE.OBJECT_ID
                            WHERE SEC_OBJECT.OBJECT_CODE = '" + objectCode + @"' 
								AND SEC_OBJECT.MENU_ID = '" + menuID + "'";
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objSecObjectRoleEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertSecObjectRole(SecObjectRoleEntity objSecObjectRoleEntity)
        {
            return DataAccess.Insert(objSecObjectRoleEntity);
        }

        public static bool InsertSecObjectRole(List<SecObjectRoleEntity> objSecObjectRoleEntityList)
        {
            return DataAccess.Insert(objSecObjectRoleEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objSecObjectRoleEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateSecObjectRole(SecObjectRoleEntity objSecObjectRoleEntity)
        {
            return DataAccess.Update(objSecObjectRoleEntity);
        }

        public static bool UpdateSecObjectRole(List<SecObjectRoleEntity> objSecObjectRoleEntityList)
        {
            return DataAccess.Update<SecObjectRoleEntity>(objSecObjectRoleEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objSecObjectRoleEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteSecObjectRole(SecObjectRoleEntity objSecObjectRoleEntity)
        {
            return DataAccess.Delete(objSecObjectRoleEntity);
        }

        public static bool DeleteSecObjectRole(List<SecObjectRoleEntity> objSecObjectRoleEntityList)
        {
            return DataAccess.Delete<SecObjectRoleEntity>(objSecObjectRoleEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetSecObjectRoleAll()
        {
            return DataAccess.SelectAllDataSet<SecObjectRoleEntity>();
        }
        #endregion
    }


}

